package com.xd.core.product.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.xd.common.constant.SysConstant;
import com.xd.common.exception.XkdException;
import com.xd.common.util.SecurityUtil;
import com.xd.core.company.mapper.CompanyMapper;
import com.xd.core.product.dto.ProductDto;
import com.xd.core.product.entity.Product;
import com.xd.core.product.entity.ProductCompany;
import com.xd.core.product.mapper.ProductCompanyMapper;
import com.xd.core.product.mapper.ProductMapper;
import com.xd.core.product.service.IProductService;
import com.xd.core.product.vo.ProductVo;
import com.xd.core.product.vo.ProductVo1;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jodd.util.StringUtil;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ZX
 * @since 2025-02-23
 */
@Api("产品管理")
@RestController
@RequestMapping("/core/product")
public class ProductController {

    @Autowired
    private IProductService iProductService;



    @PreAuthorize("hasAnyAuthority('sys:ProductManagemnet-viewDetails')")
    @ApiOperation("产品列表")
    @PostMapping("/getList")
    public Page<ProductVo> productList(@RequestBody ProductDto dto) {
        return iProductService.productList(dto);
    }


    @PreAuthorize("hasAnyAuthority('sys:AddProduct')")
    @ApiOperation("新增产品")
    @PostMapping("/insertProduct")
    public void productList(@RequestBody Product product) {

        iProductService.save(product);
    }

    @ApiOperation("产品详情")
    @GetMapping("/getDetail/{id}")
    public Product getDetail(@PathVariable Integer id) {
        Product product = iProductService.getById(id);
        if (ObjectUtil.isNotEmpty(product)) {
            // 设置产品类型备注
            switch (product.getType()) {
                case "0":
                    product.setTypeRemark("消费信贷");
                    break;
                case "1":
                    product.setTypeRemark("车抵贷");
                    break;
                case "2":
                    product.setTypeRemark("房抵贷");
                    break;
                case "3":
                    product.setTypeRemark("经营贷");
                    break;
                case "4":
                    product.setTypeRemark("保单贷");
                    break;
            }

            // 设置还款方式备注（处理多个还款方式）
            String repayment = product.getRepayment();
            if (repayment != null && !repayment.isEmpty()) {
                String[] types = repayment.split(",");
                StringBuilder remarkBuilder = new StringBuilder();

                for (String type : types) {
                    String translated;
                    switch (type) {
                        case SysConstant.REPAY_TYPE_ZERO:
                            translated = "等额本金";
                            break;
                        case SysConstant.REPAY_TYPE_ONE:
                            translated = "等额本息";
                            break;
                        case SysConstant.REPAY_TYPE_TWO:
                            translated = "等本等息";
                            break;
                        case SysConstant.REPAY_TYPE_THREE:
                            translated = "到期还本付息";
                            break;
                        case SysConstant.REPAY_TYPE_FOUR:
                            translated = "先息后本";
                            break;
                        default:
                            translated = "未知(" + type + ")";
                    }

                    if (remarkBuilder.length() > 0) {
                        remarkBuilder.append("，");
                    }
                    remarkBuilder.append(translated);
                }

                if (remarkBuilder.length() > 0) {
                    product.setRepaymentRemark(remarkBuilder.toString());
                }
            }
        }

        return product;
    }
    @PreAuthorize("hasAnyAuthority('sys:EditProduct')")
    @ApiOperation("编辑产品")
    @PostMapping("/updateProduct")
    public void updateProduct(@RequestBody Product product) {
        iProductService.updateById(product);
    }

    @ApiOperation("产品下拉框--新增进件")
    @GetMapping("/productList/{companyId}")
    public List<ProductVo1> getProductVoList(@PathVariable Integer companyId) {
        return iProductService.getProductVoList(companyId);

    }

    @ApiOperation("产品列表--新增资方配置产品费率")
    @GetMapping("/productListCapital")
    public List<ProductVo1> productListCapital() {
        return BeanUtil.copyToList(iProductService.list(), ProductVo1.class);
    }


}
